//
//  DKWCredApplyViewController.m
//  CreditHub
//
//  Created by chenglang on 2024/9/17.
//

#import "DKWCredApplyViewController.h"
#import "DKWCredApplyTableViewCell.h"
#import "HomeRequest.h"

@interface DKWCredApplyViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *arrayData;

@end

@implementation DKWCredApplyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.ch_title = @"Pressure Cohesion";
    self.navBgImageView.hidden = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"DKWCredApplyTableViewCell" bundle:nil] forCellReuseIdentifier:@"DKWCredApplyTableViewCell"];
    self.tableView.estimatedRowHeight = 160;
    [self requestInfo];
}

- (void)requestInfo{
    
    __weak typeof(self) weakSelf = self;
    [HomeRequest requestVoucherInfoSuccess:^(NSURLSessionDataTask *task, id response) {
        if([response[@"ofexcitement"] integerValue] == 0){
            weakSelf.arrayData = [NSMutableArray arrayWithArray:response[@"pity"][@"pity"]];
            [weakSelf.tableView reloadData];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arrayData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DKWCredApplyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DKWCredApplyTableViewCell" forIndexPath:indexPath];
    cell.dicInfo = self.arrayData[indexPath.row];
    __weak typeof(self) weakSelf= self;
    cell.selectBlock = ^(NSInteger index) {
        NSMutableDictionary *dic= [NSMutableDictionary dictionaryWithDictionary:weakSelf.arrayData[indexPath.row]];
        [dic setValue:@(index) forKey:@"index"];
        [weakSelf.arrayData replaceObjectAtIndex:indexPath.row withObject:dic];
        [weakSelf.tableView reloadData];
    };
    if(indexPath.row == 0){
        cell.imgViewHeader.hidden = NO;
        cell.layoutViewTop.constant = 60;
        cell.layoutTtitleTop.constant = 50;
    }else{
        cell.imgViewHeader.hidden = YES;
        cell.layoutViewTop.constant = 20;
        cell.layoutTtitleTop.constant = 20;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    return UITableViewAutomaticDimension;
    
}
- (IBAction)clickSubmit:(id)sender {
    
    NSMutableDictionary *dicInfo = [NSMutableDictionary dictionary];
    for(NSDictionary *dic in self.arrayData){
        if(!CHStringIsEmpty(dic[@"index"])){
            NSArray *select = dic[@"select"];
            NSInteger index = [dic[@"index"] integerValue];
            [dicInfo setValue:select[index] forKey: dic[@"ofexcitement"]];
        }else{
            xhl_showMessage(@"You still have unfinished options, please check carefully and complete all questions before submitting!");
            return;
        }
    }
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dicInfo options:0 error:&error];
    NSString *jsonString = @"";
    if (jsonData) {
       jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSLog(@"JSON String: %@", jsonString);
    }
    [self requestSave:jsonString];
}


- (void)requestSave:(NSString *)pity{
    
    xhl_hudLoading();
    __weak typeof(self) weakSelf = self;
    [HomeRequest requestSaveVoucherPity:pity success:^(NSURLSessionDataTask *task, id response) {
        if([response[@"ofexcitement"] integerValue] == 0){
           
            [weakSelf.navigationController popToRootViewControllerAnimated:YES];
        }
        xhl_showMessage(response[@"shared"]);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        xhl_hudLoading();
    }];
}

@end
